package com.example.usercenter.controller;

import com.example.usercenter.common.ErrorCode;
import com.example.usercenter.common.Result;
import com.example.usercenter.common.ResultUtils;
import com.example.usercenter.exception.ServiceException;
import com.example.usercenter.model.request.TagCreateRequest;
import com.example.usercenter.model.vo.RootTagVO;
import com.example.usercenter.model.vo.TagVO;
import com.example.usercenter.service.TagService;
import com.example.usercenter.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RequestMapping("/tags")
@RestController
public class TagController {

    @Resource
    private UserService userService;

    @Resource
    private TagService tagService;

    @PostMapping("/create")
    public Result<Long> createTag(@RequestBody TagCreateRequest tagCreateRequest, HttpSession session) {
        // 1. 校验参数
        if (tagCreateRequest == null) {
            throw new ServiceException(ErrorCode.PARAMS_NULL);
        }
        // 2. 新增标签业务
        Long tagId = tagService.createTag(tagCreateRequest, session);
        return ResultUtils.success(tagId);
    }

    @GetMapping("/get")
    public Result<List<TagVO>> getAllTags(HttpSession session) {
        // 获取全部标签
        List<TagVO> tagList = tagService.getAllTags(session);
        return ResultUtils.success(tagList);
    }

    @GetMapping("/list/group")
    public Result<List<RootTagVO>> getAllTagsByGroup(HttpSession session) {
        // 获取全部标签（分好组）
        List<RootTagVO> tagList = tagService.getAllTagsByGroup(session);
        return ResultUtils.success(tagList);
    }

    @GetMapping("/list/children")
    public Result<List<TagVO>> getChildrenTags(HttpSession session) {
        // 获取全部子级别标签
        List<TagVO> childrenTagList = tagService.getChildrenTags(session);
        return ResultUtils.success(childrenTagList);
    }
}
